package Admin;

import com.alibaba.fastjson.JSON;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.nio.charset.Charset;
import java.util.Map;

public class MockMvcUtils {
    public static void get(String uri, MultiValueMap<String, String> params, MockMvc mvc) {

        try{
            MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(uri).params(params)
                    .accept(MediaType.APPLICATION_JSON)) // 断言返回结果是json
                    .andReturn();// 得到返回结果

            MockHttpServletResponse response = mvcResult.getResponse();

            //拿到请求返回码
            int status = response.getStatus();

            //拿到结果
            String contentAsString = response.getContentAsString(Charset.forName("utf-8"));

            System.err.println(status);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static void post(String uri, Map<String,Object> params, MockMvc mvc){
        try {
            mvc.perform(MockMvcRequestBuilders.post(uri)
                    // 设置返回值类型为JSON,编码为：utf-8（默认）
                    .accept(MediaType.APPLICATION_JSON)
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(JSON.toJSONString(params)))
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    //判定响应类型
                    .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
                    .andExpect(MockMvcResultMatchers.jsonPath("$.code").value("10000"))
                    .andDo(MockMvcResultHandlers.print());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void doGet(MockMvc mockMvc, String uri, MultiValueMap<String, String> params){
        try {
            mockMvc.perform(
                    MockMvcRequestBuilders.get(uri)
                            .accept(MediaType.APPLICATION_JSON_VALUE)
                            .params(params)
            )
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andDo(MockMvcResultHandlers.print());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void doGet(MockMvc mockMvc, String uri){
        try {
            MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
            mockMvc.perform(
                    MockMvcRequestBuilders.get(uri)
                            .accept(MediaType.APPLICATION_JSON_VALUE)
                            .params(params)
            )
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andDo(MockMvcResultHandlers.print());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void doPost(MockMvc mockMvc, String uri, Object obj){
        try {
            mockMvc.perform(MockMvcRequestBuilders.post(uri)
                    // 设置返回值类型为JSON,编码为：utf-8（默认）
                    .accept(MediaType.APPLICATION_JSON)
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(JSON.toJSONString(obj)))
                    //判定响应类型
                    .andDo(MockMvcResultHandlers.print());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
